package com.dzt.tool.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dzt.tool.dao.interfaces.TemplateDao;
import com.dzt.tool.model.Template;
import com.dzt.tool.model.dto.AjaxObj;
import com.dzt.tool.model.dto.PagerEasyUI;
import com.dzt.tool.model.dto.TemplateDTO;
import com.dzt.tool.service.interfaces.TemplateService;

/**
 * @author tme_author
 * @date tme_date
 * @description tme_description
 */
@Service
public class TemplateServiceImpl implements TemplateService {
	@Autowired
	private TemplateDao templateDao;

	@Override
	public PagerEasyUI<TemplateDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2) throws Exception {
		StringBuffer hql = new StringBuffer(" from Template sqlAlias where 1=1 ");
		if (StringUtils.isNotEmpty(createDate1)) {
			hql.append(" and sqlAlias.createTime >= '").append(createDate1).append("' ");
		}
		if (StringUtils.isNotEmpty(createDate2)) {
			hql.append(" and sqlAlias.createTime <= '").append(createDate2).append("' ");
		}
		if (StringUtils.isNotEmpty(sort)) {
			hql.append(" order by sqlAlias.").append(sort);
		}
		if (StringUtils.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<TemplateDTO> easyUIPager = new PagerEasyUI<TemplateDTO>();
		List<Template> templates = templateDao.findByEasyUI(hql.toString(), page, rows);
		List<TemplateDTO> templateDTOs = new ArrayList<TemplateDTO>();
		TemplateDTO templateDTO = null;
		for (Template template : templates) {
			templateDTO = new TemplateDTO(template);
			templateDTOs.add(templateDTO);
		}
		easyUIPager.setRows(templateDTOs);
		easyUIPager.setTotal((Long) templateDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	private Template setTemplate(Template template, TemplateDTO templateDTO, boolean isAdd) {
		template.setStatus(templateDTO.getStatus());
		template.setOrderIndex(templateDTO.getOrderIndex());
		template.setJianpin(templateDTO.getJianpin());
		template.setQuanpin(templateDTO.getQuanpin());
		template.setName(templateDTO.getName());
		template.setIsDelete(templateDTO.getIsDelete());
		template.setId(templateDTO.getId());
		if (isAdd) {
			template.setCreateName(templateDTO.getCreateName());
			template.setCreateTime(template.getCreateTime());
		} else {
			template.setModifyName(templateDTO.getModifyName());
			template.setModifyTime(templateDTO.getModifyTime());
		}
		return template;
	}

	@Override
	public AjaxObj add(TemplateDTO templateDTO) throws Exception {
		if (templateDTO == null) {
			return new AjaxObj(0, "参数错误");
		}
		Template template = new Template();
		templateDao.add(setTemplate(template, templateDTO, true));
		return new AjaxObj(1, "增加成功");
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (StringUtils.isEmpty(ids)) {
			return new AjaxObj(0, "参数错误");
		}
		templateDao.deletes(ids);
		return new AjaxObj(1, "删除成功");
	}

	@Override
	public AjaxObj update(TemplateDTO templateDTO) throws Exception {
		if (templateDTO == null) {
			return new AjaxObj(0, "参数错误");
		}
		Template template = templateDao.load(templateDTO.getId());
		if (template == null) {
			return new AjaxObj(0, "数据库异常!");
		}
		templateDao.update(setTemplate(template, templateDTO, false));
		return new AjaxObj(1, "更新成功");
	}

}
